package com.example.hello.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.example.hello.entity.Department;
import com.example.hello.mapper.DepartmentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 部门服务层
 */
@Service
public class DepartmentService {
    
    @Autowired
    private DepartmentMapper departmentMapper;
    
    /**
     * 查询所有部门
     */
    public List<Department> getAllDepartments() {
        return departmentMapper.selectAllDepartments();
    }
    
    /**
     * 分页查询部门
     */
    public PageInfo<Department> getDepartmentsByPage(String departmentName, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Department> departments = departmentMapper.selectDepartmentsByCondition(departmentName);
        return new PageInfo<>(departments);
    }
    
    /**
     * 根据ID查询部门
     */
    public Department getDepartmentById(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("部门ID不能为空");
        }
        return departmentMapper.selectDepartmentById(id);
    }
    
    /**
     * 添加部门
     */
    public void addDepartment(Department department) {
        if (department == null || !StringUtils.hasText(department.getDepartmentName())) {
            throw new IllegalArgumentException("部门名称不能为空");
        }
        
        // 检查部门名称是否已存在
        Department existingDepartment = departmentMapper.selectDepartmentByName(department.getDepartmentName());
        if (existingDepartment != null) {
            throw new IllegalArgumentException("部门名称已存在");
        }
        
        departmentMapper.insertDepartment(department);
    }
    
    /**
     * 更新部门
     */
    public void updateDepartment(Department department) {
        if (department == null || department.getId() == null) {
            throw new IllegalArgumentException("部门ID不能为空");
        }
        
        if (!StringUtils.hasText(department.getDepartmentName())) {
            throw new IllegalArgumentException("部门名称不能为空");
        }
        
        // 检查部门是否存在
        Department existingDepartment = departmentMapper.selectDepartmentById(department.getId());
        if (existingDepartment == null) {
            throw new IllegalArgumentException("部门不存在");
        }
        
        // 检查部门名称是否与其他部门重复
        Department duplicateDepartment = departmentMapper.selectDepartmentByName(department.getDepartmentName());
        if (duplicateDepartment != null && !duplicateDepartment.getId().equals(department.getId())) {
            throw new IllegalArgumentException("部门名称已存在");
        }
        
        departmentMapper.updateDepartment(department);
    }
    
    /**
     * 删除部门
     */
    public void deleteDepartment(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("部门ID不能为空");
        }
        
        // 检查部门是否存在
        Department existingDepartment = departmentMapper.selectDepartmentById(id);
        if (existingDepartment == null) {
            throw new IllegalArgumentException("部门不存在");
        }
        
        departmentMapper.deleteDepartmentById(id);
    }
}
